package com.xjrsoft.module.version.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.version.dto.AddXjrMyappDto;
import com.xjrsoft.module.version.dto.UpdateXjrMyappDto;
import cn.dev33.satoken.annotation.SaCheckPermission;

import com.xjrsoft.module.version.dto.XjrMyappPageDto;
import com.xjrsoft.module.version.entity.XjrMyapp;
import com.xjrsoft.module.version.service.IMyappService;
import com.xjrsoft.module.version.vo.XjrMyappPageVo;

import com.xjrsoft.module.version.vo.XjrMyappVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
* @title: 我的应用
* @Author 管理员
* @Date: 2024-07-11
* @Version 1.0
*/
@RestController
@RequestMapping("/version" + "/myapp")
@Api(value = "/version"  + "/myapp",tags = "我的应用代码")
@AllArgsConstructor
public class MyappController {


    private final IMyappService myappService;

    @GetMapping(value = "/page")
    @ApiOperation(value="XjrMyapp列表(分页)")
    @SaCheckPermission("myapp:detail")
    public R page(@Valid XjrMyappPageDto dto){

        LambdaQueryWrapper<XjrMyapp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(StrUtil.isNotBlank(dto.getAppName()),XjrMyapp::getAppName,dto.getAppName())
                .like(StrUtil.isNotBlank(dto.getAppCode()),XjrMyapp::getAppCode,dto.getAppCode())
                .like(StrUtil.isNotBlank(dto.getAppType()),XjrMyapp::getAppType,dto.getAppType())
                .like(StrUtil.isNotBlank(dto.getAppVersion()),XjrMyapp::getAppVersion,dto.getAppVersion())
                .like(StrUtil.isNotBlank(dto.getAppCodetype()),XjrMyapp::getAppCodetype,dto.getAppCodetype())
                .like(StrUtil.isNotBlank(dto.getAppMemuids()),XjrMyapp::getAppMemuids,dto.getAppMemuids())
                .like(StrUtil.isNotBlank(dto.getAppInterfaceids()),XjrMyapp::getAppInterfaceids,dto.getAppInterfaceids())
                .like(StrUtil.isNotBlank(dto.getAppFormids()),XjrMyapp::getAppFormids,dto.getAppFormids())
                .like(StrUtil.isNotBlank(dto.getAppIcon()),XjrMyapp::getAppIcon,dto.getAppIcon())
                .like(StrUtil.isNotBlank(dto.getAppDicids()),XjrMyapp::getAppDicids,dto.getAppDicids())
                .like(StrUtil.isNotBlank(dto.getAppDblinkids()),XjrMyapp::getAppDblinkids,dto.getAppDblinkids())
                .like(StrUtil.isNotBlank(dto.getAppBillcodeids()),XjrMyapp::getAppBillcodeids,dto.getAppBillcodeids())
                .like(StrUtil.isNotBlank(dto.getAppRemark()),XjrMyapp::getAppRemark,dto.getAppRemark())
                    .orderByDesc(XjrMyapp::getId)
                .select(XjrMyapp.class,x -> VoToColumnUtil.fieldsToColumns(XjrMyappPageVo.class).contains(x.getProperty()));
        IPage<XjrMyapp> page = myappService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<XjrMyappPageVo> pageOutput = ConventPage.getPageOutput(page, XjrMyappPageVo.class);
        return R.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询XjrMyapp信息")
    @SaCheckPermission("myapp:detail")
    public R info(@RequestParam Long id){
        XjrMyapp xjrMyapp = myappService.getById(id);
        if (xjrMyapp == null) {
           return R.error("找不到此数据！");
        }
        return R.ok(BeanUtil.toBean(xjrMyapp, XjrMyappVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增XjrMyapp")
    @SaCheckPermission("myapp:add")
    public R add(@Valid @RequestBody AddXjrMyappDto dto){
        XjrMyapp xjrMyapp = BeanUtil.toBean(dto, XjrMyapp.class);
        boolean isSuccess = myappService.save(xjrMyapp);
        return R.ok(xjrMyapp.getId());
    }

    @PutMapping
    @ApiOperation(value = "修改XjrMyapp")
    @SaCheckPermission("myapp:edit")
    public R update(@Valid @RequestBody UpdateXjrMyappDto dto){

        XjrMyapp xjrMyapp = BeanUtil.toBean(dto, XjrMyapp.class);
        return R.ok(myappService.updateById(xjrMyapp));

    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @SaCheckPermission("myapp:delete")
    public R delete(@Valid @RequestBody List<Long> ids){
        return R.ok(myappService.removeBatchByIds(ids));

    }

}